function t(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function e(t) {
    if (Array.isArray(t)) {
        for (var e = 0, i = Array(t.length); e < t.length; e++) i[e] = t[e];
        return i;
    }
    return Array.from(t);
}

var i = function() {
    function t(t, e) {
        var i = [], a = !0, n = !1, s = void 0;
        try {
            for (var r, o = t[Symbol.iterator](); !(a = (r = o.next()).done) && (i.push(r.value), 
            !e || i.length !== e); a = !0) ;
        } catch (t) {
            n = !0, s = t;
        } finally {
            try {
                !a && o.return && o.return();
            } finally {
                if (n) throw s;
            }
        }
        return i;
    }
    return function(e, i) {
        if (Array.isArray(e)) return e;
        if (Symbol.iterator in Object(e)) return t(e, i);
        throw new TypeError("Invalid attempt to destructure non-iterable instance");
    };
}(), a = Object.assign || function(t) {
    for (var e = 1; e < arguments.length; e++) {
        var i = arguments[e];
        for (var a in i) Object.prototype.hasOwnProperty.call(i, a) && (t[a] = i[a]);
    }
    return t;
}, n = t(require("../../../utils/api")), s = t(require("../../../utils/event-emitter")), r = t(require("../../../utils/util"));

Component({
    properties: {
        app: {
            type: String,
            value: "card"
        },
        card: Object,
        origin: Object,
        switchPostion: {
            type: Boolean,
            value: !0
        },
        reserved: Number,
        positionStyle: String
    },
    data: {
        chatLists: []
    },
    ready: function() {
        var t = this, i = getApp().globalData.radarCount;
        this.setData({
            count: i > 99 ? "99+" : i
        }), this.$events = [ [ "radar.message.socket", this.handleSocket.bind(this) ], [ "chat.sendmsg", this.chatRoomSyn.bind(this) ], [ "chat.empty.reads", this.emptyChatReads.bind(this) ], [ "radar.unreads.change", function(e) {
            var i = e.count;
            return t.setData({
                count: i
            });
        } ] ], this.$events.forEach(function(t) {
            return s.default.addListener.apply(s.default, e(t));
        });
    },
    detached: function() {
        this.$events.forEach(function(t) {
            return s.default.removeListener.apply(s.default, e(t));
        }), this.getUnreadsList && clearTimeout(this.getUnreadsList), this.resetPositionUnread && clearTimeout(this.resetPositionUnread), 
        this.isClickPostion && clearTimeout(this.isClickPostion);
    },
    methods: {
        unreadsDistancesGroup: [],
        takeTurnsIndex: 0,
        nowScrollTop: 0,
        allUnreads: 0,
        onPullDownRefresh: function() {
            this.getLists(!0);
        },
        onReachBottom: function() {
            this.data.noMore || this.clickPostion || (this.setData({
                loadingMore: !0
            }), this.getLists(!1));
        },
        onPageScroll: function(t) {
            !this.clickPostion && this.data.switchPostion && (this.nowScrollTop = t || 0);
        },
        getLists: function(t, e) {
            var i = this, a = void 0;
            if (t) this.data.chatLists = [], a = parseInt(Date.now() / 1e3, 0) + 1e3; else {
                var s = this.data.chatLists;
                a = s[s.length - 1].updatetime;
            }
            n.default.chat.chat.chatlists({
                data: {
                    timestamp: a,
                    limit: 50
                },
                complete: function() {
                    wx.stopPullDownRefresh(), i.setData({
                        loadingMore: !1
                    });
                }
            }).then(function(t) {
                var a = t.data.lists;
                return i.handleLists(a, e);
            });
        },
        handleLists: function(t, e) {
            t.forEach(function(t) {
                t.time = r.default.formatTimeStamp(t.updatetime);
            });
            var i = this.data.chatLists.concat(t);
            this.setData({
                chatLists: i,
                noMore: t.length < 50
            }), t.length && this.resetPositionUnreads(e);
        },
        handleSocket: function(t) {
            if (1 != +t.issystem) {
                var e = this.data.chatLists;
                t.updatetime = t.createtime, t.time = "刚刚";
                var i = e.findIndex(function(e) {
                    return +e.chatid == +t.chatid;
                });
                i > -1 ? (t.unreads = e[i].unreads + 1, e.splice(i, 1)) : t.unreads = 1, e.unshift(t), 
                this.setData({
                    chatLists: e
                }), this.resetPositionUnreads();
            }
        },
        chatRoomSyn: function(t) {
            var e = this.data, i = e.chatLists, n = e.origin, s = a({}, t), r = i.findIndex(function(e) {
                return +t.chatid == +e.chatid;
            });
            r > -1 ? ((s = i.splice(r, 1)[0]).content = t.content, s.updatetime = t.updatetime, 
            s.time = "刚刚") : (s.uid = n.uid, s.avatar = n.avatar, s.realname = n.name), i.unshift(s), 
            this.setData({
                chatLists: i
            }), this.resetPositionUnreads();
        },
        emptyChatReads: function(t) {
            var e = this.data.chatLists, i = e.find(function(e) {
                return +t == +e.chatid;
            });
            i && i.unreads && (i.unreads = 0, this.setData({
                chatLists: e
            }), this.resetPositionUnreads());
        },
        goChat: function(t) {
            var e = t.currentTarget.dataset, i = e.name, a = e.uid, n = e.unreads, s = e.chatid, r = this.data.card, o = r ? r.cardid : "";
            wx.navigateTo({
                url: "/publicPackage/salesradar/minichat/minichat?name=" + i + "&chatid=" + s + "&touid=" + a + "&num=" + n + "&cardid=" + o + "&app=" + this.data.app
            });
        },
        collectFormId: function(t) {
            var e = t.detail.formId, i = getApp().globalData, a = i.appid, s = i.app;
            n.default.weapp.collectformid({
                data: {
                    formid: e,
                    app: s || this.data.app,
                    appid: a
                }
            });
        },
        resetPositionUnreads: function(t) {
            var e = this;
            this.data.switchPostion && (this.unreadsDistancesGroup = [], this.takeTurnsIndex = 0, 
            clearTimeout(this.resetPositionUnread), this.resetPositionUnread = setTimeout(function() {
                e.calcPositionDistance(!0).then(function() {
                    t && e.positionUnreads();
                });
            }, 200));
        },
        loadNewChatlistPostion: function() {
            var t = this;
            return !(this.data.count > this.allUnreads) || (console.info("未读消息未加载"), clearTimeout(this.getUnreadsList), 
            this.getUnreadsList = setTimeout(function() {
                t.getLists(!1, !0);
            }, 200), !1);
        },
        positionUnreads: function() {
            var t = this.unreadsDistancesGroup;
            this.loadNewChatlistPostion() && (console.log(t), (t.length ? Promise.resolve(t) : this.calcPositionDistance()).then(this.movePostion.bind(this)));
        },
        movePostion: function(t) {
            var e = this.nowScrollTop || 0, i = this.takeTurnsIndex;
            if (this.clickPostion = !0, e) {
                var a = t.map(function(t) {
                    return Math.abs(t - e);
                });
                i = a.indexOf(Math.min.apply(null, a)), this.nowScrollTop = 0, this.takeTurnsIndex = i;
            }
            wx.pageScrollTo({
                scrollTop: t[i],
                duration: 200
            }), this.resetPageScrollStatus(), t.length === i + 1 ? this.takeTurnsIndex = 0 : this.takeTurnsIndex += 1;
        },
        resetPageScrollStatus: function() {
            var t = this;
            clearTimeout(this.isClickPostion), this.isClickPostion = setTimeout(function() {
                t.clickPostion = !1;
            }, 300);
        },
        calcPositionDistance: function(t) {
            var e = this;
            t && this.getNowViewport();
            var i = 0, a = this.data.chatLists.filter(function(t) {
                return i += t.unreads, t.unreads;
            }).map(function(t) {
                return t.chatid;
            }).map(function(t) {
                return e.getNodeScroll(t);
            }), n = this.data.reserved || 0;
            return this.allUnreads = i, Promise.all(a).then(function(i) {
                return n && (i = i.map(function(t) {
                    return t - n;
                })), t && (i = i.map(function(t) {
                    return t + (e.lastScrollTop || 0);
                })), e.unreadsDistancesGroup = i, i;
            });
        },
        getNowViewport: function() {
            var t = this;
            wx.createSelectorQuery().selectViewport().scrollOffset(function(e) {
                var i = e.scrollTop;
                t.lastScrollTop = i;
            }).exec();
        },
        getNodeScroll: function(t) {
            var e = this;
            return new Promise(function(a) {
                var n = wx.createSelectorQuery().in(e);
                n.select("#chatid" + t).boundingClientRect(), n.selectViewport().scrollOffset(), 
                n.exec(function(t) {
                    var e = i(t, 1)[0].top;
                    return a(e);
                });
            });
        }
    }
});